package com.yuntsg.ruiijn.controller.admin;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.yuntsg.nnsfcp.common.utils.UserUtil;
import com.yuntsg.ruiijn.controller.system.InitController;
import com.yuntsg.ruiijn.database.entity.UnitDept;
import com.yuntsg.ruiijn.database.entity.WosBasic;
import com.yuntsg.ruiijn.database.mapper.UnitBasicMapper;
import com.yuntsg.ruiijn.database.mapper.UnitDeptMapper;
import com.yuntsg.ruiijn.database.mapper.WosBasicMapper;
import com.yuntsg.ruiijn.entity.DeptDetail;
import com.yuntsg.nnsfcp.common.beans.ResultBean;
import common.util.PageModel;
import common.util.YouDaoTransLate;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 单位管理员对于科室的管理 列出所有科室名字
 */
@RestController
@RequestMapping("admin_basic_dept")
@Slf4j
@SuppressWarnings("ALL")
//@Api(tags = "科室规范")
public class AdminBasicDeptController {

    private final String dataTable = "wos_basic";
    String unitTable = "unit_dept";
    String unknownTable = "unknown_dept";
    @Autowired
    private HttpSession session;
    @Autowired
    private UnitDeptMapper unitDeptMapper;
    @Autowired
    private WosBasicMapper wosBasicMapper;
    @Autowired
    private InitController initController;
    @Autowired
    private WosBasicController wosBasicController;
    @Autowired
    private UnitBasicMapper unitBasicMapper;

    @Resource
    UserUtil userUtil;

    //    @ApiOperation(value = "00-当前所有的文件生效科室改动")
    @GetMapping("update_dept_list")
    public ResultBean<Object> update_dept_list() throws Exception {

//
//        UnitBasic unitBasic = unitBasicMapper.selectById(sysUser.getGid());
//
//        String searchPlan = unitBasic.getSearchPlan();
//        int from = 1;
//        int max = 0;
//        List<Entity> tableNum = Db.use().query("select count(1) as count from " + dataTable + " ; ");
//        System.out.println("读表长度为:" + tableNum.get(0).getInt("count"));
//        int count = tableNum.get(0).getInt("count");
//        if (count > 0) {
//            max = Db.use().queryNumber("select max(id) from " + dataTable).intValue();
//        }
//        Integer onePageNum = 1000;
//        int page = max / onePageNum;
//        if (max % onePageNum != 0) {
//            page++;
//        }
//        System.out.println("总页数___" + page);
//        List<PageInfo> pageInfos = new ArrayList<>();
//        for (int i = 1; i <= page; i++) {
//            int start = (i - 1) * onePageNum + from;
//            int stop = i * onePageNum + from - 1;
//            if (stop > max) {
//                stop = max + onePageNum;
//            }
//            pageInfos.add(new PageInfo(start, stop));
//        }
//        pageInfos.parallelStream().forEach(pg -> {
//            List<WosBasic> list = new LambdaQueryChainWrapper<>(wosBasicMapper).eq(WosBasic::getGid, sysUser.getGid()).eq(WosBasic::getSoType, 1).between(WosBasic::getId, pg.getFrom(), pg.getTo()).list();
//            for (WosBasic wosBasic : list) {
//                wosBasicController.checkConeOrRpOrFund(wosBasic, searchPlan);
//                wosBasicMapper.updateById(wosBasic);
//            }
//        });
        initController.run();
        return new ResultBean<>();
    }


    //    @ApiOperation(value = "01-当前所有的科室名称列表")
    @GetMapping("all_dept_list")
    public ResultBean<Object> all_dept_list() {
        String loginIdAsInt = userUtil.getUidStr();
        List<String> array = new ArrayList<>();
//        List<UnitDept> list = new LambdaQueryChainWrapper<>(unitDeptMapper).eq(UnitDept::getGid, gid).list();
//        for (UnitDept unitDept : list) {
//            String info = unitDept.getInfo();
//            array = JSONObject.parseArray(info, String.class);
//            unitDept.setInfoList(array);
//            unitDept.setInfo(null);
//        }
        return new ResultBean<>();
    }

    //    @ApiOperation(value = "01-2-获取当前科室下的标识带分页")
    @GetMapping("dept_detail")
    public ResultBean<Object> dept_detail(Integer id, Integer page, Integer limit) {
        UnitDept unitDept = unitDeptMapper.selectById(id);
        String info = unitDept.getInfo();
        List<String> array = JSONObject.parseArray(info, String.class);
        List<DeptDetail> list = new ArrayList();
        for (String one : array) {
            DeptDetail deptDetail = new DeptDetail();
            deptDetail.setCnName(YouDaoTransLate.DoEnToCnTrans(one));
            deptDetail.setEnName(one);
            list.add(deptDetail);
        }

        PageModel<DeptDetail> pageModel = new PageModel<DeptDetail>(list, limit);
        List<DeptDetail> integerList = pageModel.getObjects(page);


        int allPage = pageModel.getTotalPages();
        int allSize = pageModel.getTotalRows();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("pages", allPage);//所有页数
        map.put("total", allSize);//所有条数
        map.put("current", page);//当前页
        map.put("size", limit);//每页显示数量
        map.put("list", integerList);//结果
        return new ResultBean<>(map);
    }

    //    @ApiOperation(value = "02-修改科室名字")
    @GetMapping("update_dept_name")
    public ResultBean<Object> update_dept_name(Integer id, String name) {
        UnitDept unitDept = unitDeptMapper.selectById(id);
        unitDept.setDept(name);
        unitDeptMapper.updateById(unitDept);
        return new ResultBean<>("操作成功");
    }

    //    @ApiOperation(value = "03-新增科室判断标识")
//    @GetMapping("update_dept_info")
//    public ResultBean<Object> update_dept_info(Integer id, String name) throws SQLException {
//        UnitDept unitDept = unitDeptMapper.selectById(id);
//        unitDept.setGid(gid);
//        String info = unitDept.getInfo();
//        List<String> list = JSONObject.parseArray(info, String.class);
//        list.add(name);
//        String string = JSONObject.toJSONString(list);
//        unitDept.setInfo(string);
//        unitDeptMapper.updateById(unitDept);
//        return new ResultBean<>("操作成功");
//    }

    //    @ApiOperation(value = "04-删除科室判断标识")
    @GetMapping("update_dept_info_del")
    public ResultBean<Object> update_dept_info_del(Integer id, Integer index) throws SQLException {
        UnitDept unitDept = unitDeptMapper.selectById(id);
        String info = unitDept.getInfo();
        List<String> list = JSONObject.parseArray(info, String.class);
        List<String> listnew = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (index != i) {
                listnew.add(list.get(i));
            }
        }
        String string = JSONObject.toJSONString(listnew);
        unitDept.setInfo(string);
        unitDeptMapper.updateById(unitDept);
        return new ResultBean<>("操作成功");
    }

    //    @ApiOperation(value = "05-批量指派文章给科室")
    @GetMapping("article_to_dept")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id", value = "科室ID", dataTypeClass = Integer.class),
//            @ApiImplicitParam(name = "ids", value = "文章ids 1#2#3#4", dataTypeClass = Integer.class),
//    })
    public ResultBean<Object> article_to_dept(Integer id, String ids) {
        UnitDept unitDept = unitDeptMapper.selectById(id);
        String[] split = ids.split("#");
        for (String s : split) {
            WosBasic wosBasic = wosBasicMapper.selectById(s);
            wosBasic.setDeptId(id);
            wosBasic.setDeptName(unitDept.getDept());
            wosBasicMapper.updateById(wosBasic);
        }


        return new ResultBean<>("操作成功");
    }

    //    @ApiOperation(value = "06-新增科室名称")
    @PostMapping("add_dept_info")
    public ResultBean<Object> add_dept_info(@RequestBody UnitDept unitDept) {
        String info = unitDept.getInfo();
        String[] split = info.split("###");
        List<String> list = new ArrayList<>();
        for (String s : split) {
            list.add(s);
        }
        unitDept.setInfo(JSONObject.toJSONString(list));
        unitDeptMapper.insert(unitDept);
        return new ResultBean<>("操作成功");
    }

    //    @ApiOperation(value = "07-删除科室名称")
    @GetMapping("del_dept_info")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "id"),
//    })
    public ResultBean<Object> del_dept_info(Integer id) {
        unitDeptMapper.deleteById(id);
        return new ResultBean<>("操作成功");
    }


}
